/*
 * Copyright (c) 2011 Shanda Corporation. All rights reserved.
 *
 * Created on 2011-6-3.
 */

package com.whq.common.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Stream转换通用类.
 *
 * @author wanghuaiqiang
 */
public class StreamUtil {
    private static Log LOGGER = LogFactory.getLog(StreamUtil.class);
    
    /**
     * 将 InputStream 转换为 String
     * @param is
     * @return
     */
    public static String convertStream2String(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\r\n");
            }
        } catch (IOException e) {
            LOGGER.error("读取InputStream出错: ", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                LOGGER.error("关闭InputStream出错: ", e);
            }
        }
        return sb.toString();
    }
}
